package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

type MainController struct {
	beego.Controller
}

func initServiceLogs() {
	err := beego.SetLogger("file", `{"filename":"./logs/service.log"}`)
	if err != nil {
		beego.Info(err)
		return
	}
	err = beego.BeeLogger.DelLogger("console")
	if err != nil {
		beego.Info(err)
		return
	}
}

func (c *MainController) LogInfo(content string) {
	log := logs.NewLogger()               //日志对象
	log.SetLevel(logs.LevelInformational) //设置日志级别
	err := log.SetLogger("file", `{"filename":"./logs/query.log"}`)
	if err != nil {
		beego.Error(err)
		c.Redirect("/", 302)
	}
	err = log.DelLogger("console")
	if err != nil {
		beego.Error(err)
		c.Redirect("/", 302)
	}
	log.Info(content)
}

func (c *MainController) Get() {
	initServiceLogs()
	c.TplName = "index.html"
}

func (c *MainController) Post() {
	initServiceLogs()
	content := c.GetString("Content")
	c.LogInfo(content)
}
